local faheng = fk.CreateSkill {
  name = "hanqing__faheng",
}

Fk:loadTranslationTable{
  ["hanqing__faheng"] = "法衡",
  [":hanqing__faheng"] = "你的红色牌进入中央区后，你可以弃置一名角色一张牌，若中央区花色数增至四的时机在此：<br>"..
  "发动之前，此技能本回合失效；<br>发动期间，其回复1点体力；<br>仍未发生，你弃置一张牌。",

  ["#hanqing__faheng-choose"] = "法衡：你可以弃置一名角色一张牌",
}

local fahengJudge = function (room)
  local suits = {}
  for _, id in ipairs(room:getBanner("@$CenterArea")) do
    table.insertIfNeed(suits, Fk:getCardById(id).suit)
  end
  table.removeOne(suits, Card.NoSuit)
  return #suits == 4
end

faheng:addEffect(fk.AfterCardsMove, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(faheng.name) and player.room:getBanner("@$CenterArea") and
      table.find(player.room.alive_players, function (p)
        return not p:isNude()
      end) then
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile then
          if move.from == player then
            for _, info in ipairs(move.moveInfo) do
              if table.contains(player.room:getBanner("@$CenterArea"), info.cardId) and
                (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) and
                Fk:getCardById(info.cardId).color == Card.Red then
                return true
              end
            end
          elseif move.from == nil then
            if table.contains({fk.ReasonUse, fk.ReasonResponse}, move.moveReason) then
              local parent_event = player.room.logic:getCurrentEvent().parent
              if parent_event ~= nil then
                local card_ids = {}
                if parent_event.event == GameEvent.UseCard or parent_event.event == GameEvent.RespondCard then
                  local use = parent_event.data
                  if use.from == player then
                    parent_event:searchEvents(GameEvent.MoveCards, 1, function(e2)
                      if e2.parent and e2.parent.id == parent_event.id then
                        for _, move2 in ipairs(e2.data) do
                          if move2.from == player and (move2.moveReason == fk.ReasonUse or move2.moveReason == fk.ReasonResponse) then
                            for _, info in ipairs(move2.moveInfo) do
                              if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) then
                                table.insertIfNeed(card_ids, info.cardId)
                              end
                            end
                          end
                        end
                      end
                    end)
                  end
                end
                if #card_ids > 0 then
                  for _, info in ipairs(move.moveInfo) do
                    if table.contains(card_ids, info.cardId) and
                      table.contains(player.room:getBanner("@$CenterArea"), info.cardId) and
                      Fk:getCardById(info.cardId).color == Card.Red then
                      return true
                    end
                  end
                end
              end
            elseif move.moveReason == fk.ReasonPutIntoDiscardPile then
              local pindian_event = player.room.logic:getCurrentEvent().parent:findParent(GameEvent.Pindian)
              if pindian_event then
                local card_ids = {}
                pindian_event:searchEvents(GameEvent.MoveCards, 1, function(e2)
                  if e2.parent and e2.parent.id == pindian_event.id then
                    for _, move2 in ipairs(e2.data) do
                      if move2.from == player and move2.moveReason == fk.ReasonPut and move2.toArea == Card.Processing then
                        for _, info in ipairs(move2.moveInfo) do
                          if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) then
                            table.insertIfNeed(card_ids, info.cardId)
                          end
                        end
                      end
                    end
                  end
                end)
                if #card_ids > 0 then
                  for _, info in ipairs(move.moveInfo) do
                    if table.contains(card_ids, info.cardId) and
                      table.contains(player.room:getBanner("@$CenterArea"), info.cardId) and
                      Fk:getCardById(info.cardId).color == Card.Red then
                      return true
                    end
                  end
                end
              end
            end
          end
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function(p)
      return not p:isNude()
    end)
    if table.contains(targets, player) and
      not table.find(player:getCardIds("he"), function (id)
        return not player:prohibitDiscard(id)
      end) then
      table.removeOne(targets, player)
    end
    if #targets == 0 then
      room:askToCards(player, {
        min_num = 1,
        max_num = 1,
        include_equip = false,
        skill_name = faheng.name,
        pattern = "false",
        prompt = "#hanqing__faheng-choose",
        cancelable = true,
      })
    else
      local to = room:askToChoosePlayers(player, {
        min_num = 1,
        max_num = 1,
        targets = targets,
        skill_name = faheng.name,
        prompt = "#hanqing__faheng-choose",
        cancelable = true,
      })
      if #to > 0 then
        event:setCostData(self, {tos = to})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local yes1 = fahengJudge(room)
    if to == player then
      room:askToDiscard(player, {
        min_num = 1,
        max_num = 1,
        include_equip = true,
        skill_name = faheng.name,
        cancelable = false,
      })
    else
      local id = room:askToChooseCard(player, {
        target = to,
        flag = "he",
        skill_name = faheng.name,
      })
      room:throwCard(id, faheng.name, to, player)
    end
    local yes2 = fahengJudge(room)
    if yes1 then
      room:invalidateSkill(player, faheng.name, "-turn")
    else
      if yes2 then
        if not to.dead then
          room:recover{
            who = to,
            num = 1,
            recoverBy = player,
            skillName = faheng.name,
          }
        end
      else
        if not player.dead then
          room:askToDiscard(player, {
            min_num = 1,
            max_num = 1,
            include_equip = true,
            skill_name = faheng.name,
            cancelable = false,
          })
        end
      end
    end
  end,
})

faheng:addAcquireEffect(function (self, player)
  player.room:addSkill("#CenterArea")
end)

return faheng
